package com.yueqian.Service;

import com.yueqian.Dao.GetEmployee;
import com.yueqian.Entity.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/GetEmployeeService,/page")
public class GetEmployeeService extends HttpServlet {
    /**
     *接受到请求则直接返回数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        GetEmployee getEmployee = new GetEmployee();
//        List<Employee> allEmployee = getEmployee.getAllEmployee();
//        if(allEmployee != null)
//        {
//            System.out.println("存在员工信息");
//            req.setAttribute("employees",allEmployee);
//        }
//        else {
//            System.out.println("没有员工信息");
//             req.setAttribute("employees", null);
//        }
//        //转发网页，并发信息域转发
//        req.getRequestDispatcher("GetEmployee.jsp").forward(req,resp);
//    }

    /**
     * 条件查询
     */
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//             req.setCharacterEncoding("UTF-8");
//        String username = req.getParameter("username");
//        String number = req.getParameter("number");
//        System.out.println("用户名："+username+"编号："+number);
//        GetEmployee getEmployee = new GetEmployee();
//        List<Employee> allEmployee = new ArrayList<>();
//        if(username == "" && number == ""){
//            allEmployee=getEmployee.getAllEmployee();
//            if(allEmployee != null)
//            {
//                System.out.println("存在员工信息");
//                req.setAttribute("employees",allEmployee);
//            }
//            else {
//                System.out.println("没有员工信息");
//                req.setAttribute("employees", null);
//            }
//            //转发网页，并发信息域转发
//            req.getRequestDispatcher("GetEmployee.jsp").forward(req,resp);
//        }
//
//        else
//        {
//        Employee employee = getEmployee.getEmployee(username,number);
//            boolean add = allEmployee.add(employee);
//            if(add){
//                req.setAttribute("employees",allEmployee);
//            }
//            else {
//                req.setAttribute("employees",null);
//            }
//            req.getRequestDispatcher("GetEmployee.jsp").forward(req,resp);
//    }
//        }

    /**
     * 分页查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前的页号
        int pageIndex=1;
        //每页显示的记录数
        int pageSize=3;
        //获取页面上传递过来的页pageNo值
        String pageNoStr = req.getParameter("pageNo");
        if(pageNoStr == null ){
            pageNoStr = "1";//初始值为1
        }else{
            pageIndex = Integer.parseInt(pageNoStr);//转换为Int类型
        }
        //得到总记录数
        GetEmployee getEmployee = new GetEmployee();
        int totalCount = getEmployee.getTotalCount();
        System.out.println("总记录数："+totalCount);
        int totalPages = (int) Math.ceil(totalCount*1.0/pageSize); //向上取整
        System.out.println("总页数："+totalPages);
        //判断pageIndex是否是第一页
        if(pageIndex<=1) {
            pageIndex=1;  //如果已经是第一页了，再点击上一页时，还是显示第一页的内容
        }
        //判断pageIndex是否是最后一页
        if(pageIndex>=totalPages) {
            pageIndex=totalPages; //如果已经是最后一页了，再点击下一页时，还是显示最后一页的内容
        }
        //查询当前的记录
        List<Employee> pageUsers = getEmployee.getPageUsers(pageIndex,pageSize);

        //把当前页号和总页数存储在request域对象当中，传递到显示页面
        req.setAttribute("pageIndex", pageIndex);
        req.setAttribute("totalPages", totalPages);
        //转发
        req.getRequestDispatcher("emplist.jsp").forward(req, resp);
    }
}
